package com.example.stumanager.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.stumanager.R;
import com.example.stumanager.entity.LateReturnEntity;

import java.util.List;

/**
 * 晚归打卡适配器
 */
public class LateReturnAdapter extends BaseAdapter {

    private List<LateReturnEntity> data; //friend数据
    private Context context;
    private LayoutInflater layoutInflater;

    //初始化数据，并获取主窗口的context,获取friend数据，之后获取context的布局服务
    public LateReturnAdapter(Context context, List<LateReturnEntity> data) {
        this.data = data;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    //获取friend的数目
    @Override
    public int getCount() {
        return data.size();
    }

    //获取第i个friend的数据
    @Override
    public LateReturnEntity getItem(int i) {
        return data.get(i);
    }

    //获取第i个friend的id
    @Override
    public long getItemId(int i) {
        return i;
    }

    //设置每一项friend item的UI
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        //加载item布局
        convertView = layoutInflater.inflate(R.layout.clockinlate_adapter, null);

        //获取item布局的子控件，将friend的数据设置到控件上
        //设置学号
        TextView number = (TextView) convertView.findViewById(R.id.number);
        number.setText("学号：" + (String) data.get(position).getNumber());
        //设置图片
        ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
        Bitmap bitmap1 = stringToBitmap(data.get(position).getFace());
        imageView.setImageBitmap(bitmap1);
        //设置名字
        TextView name = (TextView) convertView.findViewById(R.id.name);
        name.setText("名字：" + (String) data.get(position).getName());
        //设置是否已报备宿管
        TextView state = (TextView) convertView.findViewById(R.id.state);
        state.setText("是否报备宿管：" + (String) data.get(position).getState());
        //设置晚归说明
        TextView explains = (TextView) convertView.findViewById(R.id.explains);
        explains.setText("晚归说明：" + (String) data.get(position).getExplains());

        return convertView;
    }

    //获取图片
    public static Bitmap stringToBitmap(String string) {
        //将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}

